package com.shop.service;

import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.JSON2.json.JSONArray;
import com.JSON2.json.JSONObject;
import com.shop.daoImpl.AddressDaoImpl;
import com.shop.daoImpl.CarGoodsListDaoImpl;
import com.shop.daoImpl.CollectionDaoImpl;
import com.shop.daoImpl.GoodsDaoImpl;
import com.shop.daoImpl.MemberDaoImpl;
import com.shop.daoImpl.MerchantAddressDaoImpl;
import com.shop.entity.Address;
import com.shop.entity.CarGoodsList;
import com.shop.entity.Collection;
import com.shop.entity.Footprint;
import com.shop.entity.Goods;
import com.shop.entity.Member;
import com.shop.entity.MemberInfor;
import com.shop.entity.MerchantAddress;
import com.shop.entity.MerchantOrder;
import com.shop.entity.Order;
import com.shop.entity.OrderGoodsList;
import com.shop.entity.OrderLogistics;

/** 
 * <b>一句话描述该类<b>
 * @author 威 
 * <br>2018年3月15日 下午3:50:08 
 *  
 */
@Service
public class MemberServiceImpl {
	@Resource(name="memberDaoImpl")
	private MemberDaoImpl memberDaoImpl;
	@Resource(name="goodsDaoImpl")
	private GoodsDaoImpl goodsDaoImpl;
	@Resource(name="carGoodsListDaoImpl")
	private CarGoodsListDaoImpl carGoodsListDaoImpl;
	@Resource(name="collectionDaoImpl")
	private CollectionDaoImpl collectionDaoImpl;
	@Resource(name="merchantAddressDaoImpl")
	private MerchantAddressDaoImpl merchantAddressDaoImpl;
	@Resource(name="addressDaoImpl")
	private AddressDaoImpl addressDaoImpl;

	public void setCollectionDaoImpl(CollectionDaoImpl collectionDaoImpl) {
		this.collectionDaoImpl = collectionDaoImpl;
	}
	public void setCarGoodsListDaoImpl(CarGoodsListDaoImpl carGoodsListDaoImpl) {
		this.carGoodsListDaoImpl = carGoodsListDaoImpl;
	}
	public void setGoodsDaoImpl(GoodsDaoImpl goodsDaoImpl) {
		this.goodsDaoImpl = goodsDaoImpl;
	}
	public void setMemberDaoImpl(MemberDaoImpl memberDaoImpl) {
		this.memberDaoImpl = memberDaoImpl;
	}
	public void setMerchantAddressDaoImpl(MerchantAddressDaoImpl merchantAddressDaoImpl) {
		this.merchantAddressDaoImpl = merchantAddressDaoImpl;
	}
	public void setAddressDaoImpl(AddressDaoImpl addressDaoImpl) {
		this.addressDaoImpl = addressDaoImpl;
	}
	public String addColle(Integer member_id, Integer goods_id){
		Member member = memberDaoImpl.findById(member_id);
		Set<Collection> colles = member.getCollection();
		boolean bool = false;
		JSONObject object = new JSONObject();
		for(Collection colle : colles){
			if(colle.getGoods_id() == goods_id) {
				bool = true;
				object.put("isExit", true);
			}
		}
		if(!bool) {
			object.put("isExit", false);
			Collection colle2 = new Collection(); 
			colle2.setGoods_id(goods_id);
			member.getCollection().add(colle2);
			
			memberDaoImpl.save(member);
			collectionDaoImpl.save(colle2);
			
			object.put("id", colle2.getId());
			Goods goods = goodsDaoImpl.findById(goods_id);
			object.put("img", goods.getImgPath());
			object.put("name", goods.getName());
			object.put("price", goods.getPrice());
		}
		return object.toString();
	}
	public String addCar(Integer member_id, JSONObject obj){
		Integer id = (Integer) obj.get("id");
		String size = (String) obj.get("size");
		String color = (String) obj.get("color");
		Member member = memberDaoImpl.findById(member_id);
		Goods g = goodsDaoImpl.findById(id);
		Set<CarGoodsList> goodss = member.getCarGoodsList();
		boolean bool = false;
		
		Integer carGoodsId = 0;
		Integer quantityNum = 1;
		boolean isExit = false;
		for(CarGoodsList goods : goodss){
			if(goods.getGoods_id() == id){
				bool = true;
				Integer count = goods.getQuantity();
				quantityNum = count + 1;
				goods.setQuantity(quantityNum); 
				carGoodsId = goods.getId();
				isExit = true;
				carGoodsListDaoImpl.save(goods);
			}
		}
		CarGoodsList carGoods3;
		if(!bool) {
			carGoods3 = new CarGoodsList(); 
			carGoods3.setColor(color);
			carGoods3.setSize(size);
			carGoods3.setQuantity(1);
			carGoods3.setGoods_id(id);
			carGoods3.setMember(member);
			
			member.getCarGoodsList().add(carGoods3);
			
			memberDaoImpl.save(member);
			carGoodsListDaoImpl.save(carGoods3);
			carGoodsId = carGoods3.getId();
		}
		JSONObject object = new JSONObject();
		
		object.put("isExit", isExit);
		object.put("id", carGoodsId);
		object.put("img", g.getImgPath());
		object.put("name", g.getName());
		object.put("size", size);
		object.put("quantity", quantityNum);
		object.put("price", g.getPrice());
		return object.toString();
	}
	public JSONArray getOrder (Integer id){
		Member member = memberDaoImpl.findById(id);
		Set<Order> orders = member.getOrder();
		
		JSONArray orderArr = new JSONArray();
		JSONObject orderObject;
		for(Order order : orders){
			System.out.println(order.getId()); 
			//璁㈠崟鏈韩
			orderObject = new JSONObject();
			JSONObject obj = new JSONObject();
			obj.put("id", order.getId());
			obj.put("time", order.getDate());
			obj.put("money", order.getTotalAmount());
			orderObject.put("obj", obj);
			System.out.println("obj - "+obj.toString());
			//鍟嗗order
			Set<MerchantOrder> morders = order.getMerchantOrders();
			JSONArray busiArr = new JSONArray();
			JSONObject busi;
			for(MerchantOrder morder : morders){
				busi = new JSONObject();
				
				busi.put("id", morder.getId());
				busi.put("storeName", morder.getMerchant().getMerchantInfor().getName());
				busi.put("deliveryState", morder.isDeliveryState());
				busi.put("progressState", morder.isProgressState());
				//鍦板潃
				OrderLogistics orderLogistics =  morder.getLogistics();
				JSONObject logistics = new JSONObject();  System.out.println("getOrder 瑁呭叆瀵硅薄2.5");
				Integer merAddressId = orderLogistics.getMerchantAddressId();
				MerchantAddress merchantAddress = merchantAddressDaoImpl.findById(merAddressId); 
				logistics.put("shipAddress", merchantAddress.getProvince() + "-" + merchantAddress.getCity() + "-" + merchantAddress.getStreet());
				
				Integer memAddressId = orderLogistics.getMemeberAddressId();
				Address address_ = addressDaoImpl.findById(memAddressId);
				logistics.put("receiptAddress", address_.getProvince() + "-" + address_.getCity() + "-" + address_.getStreet());
				
				busi.put("logistics", logistics); System.out.println("getOrder 瑁呭叆瀵硅薄3");
				System.out.println("busi - "+busi.toString()); System.out.println("getOrder 瑁呭叆瀵硅薄4");
				
				
				//鍟嗗搧
				JSONArray goodsArr = new JSONArray();
				JSONObject goodsObject;
				Set<OrderGoodsList> gs = morder.getOrderGoodsLists();  System.out.println("getOrder 瑁呭叆瀵硅薄3");
				for(OrderGoodsList g : gs){
					goodsObject = new JSONObject();
					Goods goods = goodsDaoImpl.findById(g.getGoods_id());  System.out.println("getOrder 瑁呭叆瀵硅薄4");
					goodsObject.put("id", g.getId());
					goodsObject.put("path", goods.getImgPath());
					goodsObject.put("name", goods.getName());
					goodsObject.put("price", goods.getPrice());
					goodsObject.put("size", g.getSize());
					goodsObject.put("color", g.getColor()); System.out.println("getOrder 瑁呭叆瀵硅薄5");
					goodsObject.put("count", g.getQuantity());
					System.out.println("goodsObject - "+goodsObject.toString());
					goodsArr.add(goodsObject);
				}
				busi.put("goods", goodsArr); System.out.println("getOrder 瑁呭叆瀵硅薄6");
				busiArr.add(busi);
			}
			orderObject.put("busi", busiArr);
			orderArr.add(orderObject); System.out.println("getOrder 瑁呭叆瀵硅薄7");	
		}
		return orderArr;
	}
	public Object getColle(Integer id){
		Member member = memberDaoImpl.findById(id);
		System.out.println("coll");
		Set<Collection> colls = member.getCollection();
		JSONArray collArr = new JSONArray();System.out.println("coll");
		JSONObject coolObj;
		for(Collection coll : colls){
			coolObj = new JSONObject();
			coolObj.put("time", coll.getDate());
			System.out.println("coll" + coll.getGoods_id());
			Goods goods = goodsDaoImpl.findById(coll.getGoods_id()); 
			System.out.println("coll");
			coolObj.put("id", coll.getId());
			
			coolObj.put("price", goods.getPrice());
			coolObj.put("name", goods.getName());
			coolObj.put("img", goods.getImgPath());
			collArr.add(coolObj);
		}
		System.out.println("coll end");
		//System.out.println("coll"+collArr.toString());
		return collArr;
	}
	
	public Object getFoot(Integer id){
		Member member = memberDaoImpl.findById(id);
		
		Set<Footprint> foots = member.getFootprint();
		JSONArray collArr = new JSONArray();
		JSONObject coolObj;
		for(Footprint foot : foots){
			coolObj = new JSONObject();
			coolObj.put("time", foot.getDate());
			Goods goods = goodsDaoImpl.findById(foot.getGoods_id()); 
			coolObj.put("id", foot.getId());
			coolObj.put("price", goods.getPrice());
			coolObj.put("name", goods.getName());
			coolObj.put("img", goods.getImgPath());
			collArr.add(coolObj);
		}
		return collArr;
	}
	public Object getCar(Integer id){
		Member member = memberDaoImpl.findById(id);
		
		Set<CarGoodsList> goodss = member.getCarGoodsList();
		JSONArray collArr = new JSONArray();
		JSONObject coolObj;
		for(CarGoodsList g : goodss){
			coolObj = new JSONObject();
			Goods goods = goodsDaoImpl.findById(g.getGoods_id()); 
			coolObj.put("id", g.getId());
			coolObj.put("price", goods.getPrice());
			coolObj.put("name", goods.getName());
			coolObj.put("img", goods.getImgPath());
			coolObj.put("discount", goods.getDiscount());
			coolObj.put("color", g.getColor());
			coolObj.put("size", g.getSize());
			coolObj.put("quantity", g.getQuantity());
			collArr.add(coolObj);
		}
		return collArr;
	}
	public Object getInfor(Integer id){
		System.out.println("getInfor");
		JSONObject initDataJSONObject = new JSONObject();
		Member member = memberDaoImpl.findById(id);
		
		//获取用户基本信息
		MemberInfor infor = member.getInfor();
		initDataJSONObject.put("Name", member.getUsername());
		if(infor != null){
			System.out.println("infor-"+infor);
			initDataJSONObject.put("integral", infor.getIntegral());
			initDataJSONObject.put("trueName", infor.getTrueName());
			initDataJSONObject.put("sex", infor.getSex());
			initDataJSONObject.put("pNumber", infor.getPhone_number());
			initDataJSONObject.put("email", infor.getEmail());
		} 
		
		//获取地址信息
		Set<Address> addressSet = member.getAddress();
		JSONArray addressJSONArray = new JSONArray();
		if(addressSet.size() > 0){
			JSONObject addressJSONObject;
			for(Address ad : addressSet){
				addressJSONObject = new JSONObject();
				addressJSONObject.put("id", ad.getId());
				addressJSONObject.put("province", ad.getProvince());
				addressJSONObject.put("city", ad.getCity());
				addressJSONObject.put("street", ad.getStreet());
				addressJSONArray.add(addressJSONObject); 
			}
		}
		initDataJSONObject.put("address", addressJSONArray);
		//测试输出
		System.out.println(initDataJSONObject.toString());
		return initDataJSONObject;
	}
	public Object[] login(String username, String password){
		return memberDaoImpl.findByUsername(username, password);
	}
	public boolean register(JSONObject obj){
		Member member = new Member();
		member.setUsername((String) obj.get("username"));
		member.setPassword((String) obj.get("password"));
		
		MemberInfor infor = new MemberInfor();
		infor.setEmail((String) obj.get("email"));
		infor.setIntegral("10");
		infor.setLevel(0);
		infor.setPhone_number((String) obj.get("phoneNumber"));
		infor.setSex((String) obj.get("sex"));
		infor.setTrueName((String) obj.get("trueName"));
		infor.setMember(member);
		return memberDaoImpl.saveInfor(member, infor);
	} 
}
